/**
 * 
 */
package cn.jy.map.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.jy.map.service.VillageColligateService;
import cn.jy.map.utils.MapUtils;

/**
 * @author 刘中斐
 *@date 2018年7月5日 上午11:31:21 
 */
@Controller
public class PulpController {
	@Autowired
	private VillageColligateService villageColligateService;
	@RequestMapping("newmap/getPulpWeight")	
	public @ResponseBody Map<String, Object> getPulpWeight (HttpServletRequest request, HttpServletResponse response) throws Exception {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
	
		Map<String, Object> map = MapUtils.thisLastPulp;
		if (map != null && map.size() > 0) {

		} else {
			map = villageColligateService.getPulpWeight(startTime, endTime);
			MapUtils.thisLastPulp = map;
		}
		String callback = request.getParameter("callback");
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(map);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}

	@RequestMapping("newmap/getPulp")	
	public @ResponseBody Map<String, Object> getPulp (HttpServletRequest request, HttpServletResponse response) throws Exception {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
			
		String callback = request.getParameter("callback");
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(villageColligateService.getPulpInterval());

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}
}
